package com.example.benefitcommunity.service.impl;

import com.example.benefitcommunity.entity.BcAddressList;
import com.example.benefitcommunity.dao.BcAddressListDao;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcAddressListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通讯录表(BcAddressList)表服务实现类
 *
 * @author makejava
 * @since 2025-08-06 15:07:28
 */
@Transactional(rollbackFor = Exception.class)
@Service("bcAddressListService")
public class BcAddressListServiceImpl implements BcAddressListService {
    @Resource
    private BcAddressListDao bcAddressListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BcAddressList queryById(Integer id) {
        return this.bcAddressListDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param bcAddressList 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @Override
    public Page<BcAddressList> queryByPage(BcAddressList bcAddressList, PageRequest pageRequest) {
        long total = this.bcAddressListDao.count(bcAddressList);
        return new PageImpl<>(this.bcAddressListDao.queryAllByLimit(bcAddressList, pageRequest), pageRequest, total);
    }

    /**
     * @param name 姓名
     * @return
     */
    @Override
    public Result<Object> queryByName(String name) {
        Map<String, Object> map = new HashMap<>();
        BcAddressList bcAddressList = new BcAddressList();
        bcAddressList.setName(name);
        List<BcAddressList> list = this.bcAddressListDao.queryAll(bcAddressList);
        map.put("AddressList", list);
        map.put("module", "通讯录");
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) list.size(), map);
    }

    /**
     * 通过查询数据
     *
     * @param name  姓名
     * @param phone 类型
     * @param pagenum 页码
     * @param pagesize 页大小
     * @return 数据
     */
    @Override
    public Result<Object> queryByAddressList(String name, String phone, Integer communityid, Integer pagenum, Integer pagesize) {
        PageHelper.startPage(pagenum, pagesize);
        BcAddressList bcAddressList = new BcAddressList();
        bcAddressList.setName(name);
        bcAddressList.setPhone(phone);
        bcAddressList.setCommunityid(communityid);
        List<BcAddressList> list = this.bcAddressListDao.queryAll(bcAddressList);
        PageInfo<BcAddressList> pageInfo = new PageInfo<>(list);
        return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param bcAddressList 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> insert(BcAddressList bcAddressList) {
        List<BcAddressList> list = this.bcAddressListDao.queryAll(bcAddressList);
        if (list.isEmpty()) {
            if (this.bcAddressListDao.insert(bcAddressList) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "通讯录新增成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通讯录新增失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通讯录已存在！");
    }

    /**
     * 修改数据
     *
     * @param bcAddressList 实例对象
     * @return 实例对象
     */
    @Override
    public Result<Object> update(BcAddressList bcAddressList) {
        BcAddressList bcAddressListNew = this.bcAddressListDao.queryById(bcAddressList.getId());
        if (bcAddressListNew != null) {
            if (this.bcAddressListDao.update(bcAddressList) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, "通讯录更新成功！");
            }
            return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通讯录更新失败！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通讯录不存在！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result<Object> deleteById(Integer[] id) {
        int count = 0;
        //删除个别
        for (Integer i : id) {
            //删除单个
            if (this.bcAddressListDao.deleteById(i) > 0) {
                count++;
            }
        }
        //判断删除结果
        if (count == id.length) {
            return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), (long) id.length, "通讯录删除成功！");
        }
        return new Result<>(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMsg(), 0L, "通讯录删除失败！");
    }
}
